package patty.demo;
import java.util.Arrays;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/*
 * TRequest URL:http://web.trtc.com.tw/c/2stainfo.asp
 * Request Method:POST
 * Status Code:200 OK
 * Request Headersview source
 * Content-Type:application/x-www-form-urlencoded
 * Host:web.trtc.com.tw
 * Origin:http://web.trtc.com.tw
 * Referer:http://web.trtc.com.tw/c/2stainfo.asp
 Form Dataview sourceview URL encoded
 s1elect:027
 action:query
 s2elect:023
 submit: 確定
 */

/**
 * 
 * @author Patty
 */
public class UniWinner {

	public static void main(String[] args) {
		Document doc;
		try {
			Connection connection = Jsoup.connect("http://www.dot.gov.tw/dot/UIN.jsp");
			doc = connection.userAgent("Mozilla").get();
			//System.out.println(doc);

			// get page title
			String title = doc.title();
			System.out.println(title);
			
			// 取得中獎月份
			Elements tableMonth = doc.select("h2");
			for (Element e : tableMonth) {
				if(e.text().indexOf("年")>=0){
					System.out.println(e.text());
				}
			}
			
			// 取得中獎號碼資料
			Elements tableData = doc.select("td");

			for (Element td : tableData) {
				String classString = td.attr("class");
				if (classString != null && classString.length() > 0) {
					System.out.println("***********************");
					System.out.println(classString + " : " + td.text());
				} else {
					System.out.println("data : " + td.text());
					Elements spanData = td.select("span");
					for (Element span : spanData) {
						String[] winnings = span.text().split("、");
						System.out.println("中獎號碼 : "+ Arrays.toString(winnings));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
